package com.back.controller;

import com.back.common.utils.Result;
import com.back.common.utils.SecurityUtil;
import com.back.dto.CompanyRecruiterDTO;
import com.back.entity.CompanyRecruiter;
import com.back.service.ICompanyRecruiterService;
import com.back.service.ICompanyService;
import com.back.vo.CompanyRecruiterVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  企业招聘人员前端控制器
 * </p>
 *
 * @author liyang
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/company/{companyId}/recruiter")
public class CompanyRecruiterController {

    @Autowired
    private ICompanyRecruiterService recruiterService;

    @Autowired
    private ICompanyService companyService;

    /**
     * 获取企业招聘人员列表
     */
    @GetMapping("/list")
    public Result getRecruiterList(
            @PathVariable Long companyId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!SecurityUtil.hasRole("ADMIN") && !companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        Page<CompanyRecruiter> page = new Page<>(current, size);
        IPage<CompanyRecruiterVO> recruiterList = recruiterService.getRecruiterList(page, companyId);
        return Result.ok().data("recruiterList", recruiterList);
    }

    /**
     * 添加企业招聘人员
     */
    @PostMapping
    public Result addRecruiter(@PathVariable Long companyId, @RequestBody @Valid CompanyRecruiterDTO recruiterDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!recruiterService.isCompanyAdmin(companyId, userId)) {
            return Result.error().message("只有企业管理员可以添加招聘人员");
        }
        recruiterDTO.setCompanyId(companyId);
        CompanyRecruiterVO recruiter = recruiterService.addRecruiter(recruiterDTO);
        return Result.ok().data("recruiter", recruiter);
    }

    /**
     * 更新招聘人员信息
     */
    @PutMapping("/{id}")
    public Result updateRecruiter(
            @PathVariable Long companyId,
            @PathVariable Long id,
            @RequestBody @Valid CompanyRecruiterDTO recruiterDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!recruiterService.isCompanyAdmin(companyId, userId)) {
            return Result.error().message("只有企业管理员可以更新招聘人员信息");
        }
        CompanyRecruiterVO recruiter = recruiterService.updateRecruiter(companyId, id, recruiterDTO);
        return Result.ok().data("recruiter", recruiter);
    }

    /**
     * 删除招聘人员
     */
    @DeleteMapping("/{id}")
    public Result deleteRecruiter(@PathVariable Long companyId, @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!recruiterService.isCompanyAdmin(companyId, userId)) {
            return Result.error().message("只有企业管理员可以删除招聘人员");
        }
        boolean success = recruiterService.deleteRecruiter(companyId, id);
        return success ? Result.ok() : Result.error().message("删除失败");
    }
}
